﻿
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SanguoSharp.Server
{
    public sealed class GameTable
    {
        private Game game;
        private Dictionary<int, Card> cards;
        private Stack<Card> graveyard;
        private Stack<Card> deck;
        private List<Card> selection;
        private Player selectionOwner;

        public Card GraveyardTop
        {
            get { return graveyard.Count == 0 ? null : graveyard.Peek(); }
        }
        public ReadOnlyCollection<Card> Selection
        {
            get { return new ReadOnlyCollection<Card>(selection); }
        }
        public ReadOnlyCollection<ICard> GetSelection(Player player)
        {
            if (player == selectionOwner || selectionOwner == null)
                return new ReadOnlyCollection<ICard>(selection.ConvertAll<ICard>(c => c));
            else
                return new ReadOnlyCollection<ICard>(selection.ConvertAll<ICard>(c => c.Empty));
        }

        public Game Game
        {
            get { return game; }
        }

        public IEnumerable<Card> AllCards
        {
            get { return cards.Values; }
        }

        public GameTable(Game game)
        {
            this.game = game;
            selection = new List<Card>();
            GenerateCards();
        }

        public Card GetCard(int id)
        {
            try
            {
                return cards[id];
            }
            catch (KeyNotFoundException)
            {
                throw new InvalidIdException();
            }
        }

        /// <summary>
        /// 发牌
        /// </summary>
        public void Deal()
        {
            bool repeat;
            do
            {
                repeat = false;
                foreach (Player p in game.Players)
                {
                    int d = p.InitialCardCount - p.Hand.Count;
                    if (d > 0)
                    {
                        PlayerDrawFromDeck(p, d >= 2 ? 2 : d);
                        repeat = true;
                    }
                }
            }
            while (repeat);
        }

        /// <summary>
        ///  从牌堆摸牌
        /// </summary>
        /// <param name="player">玩家</param>
        /// <param name="count">数量</param>
        /// <param name="revealCards">展示摸的牌</param>
        /// <returns></returns>
        public ReadOnlyCollection<Card> PlayerDrawFromDeck(Player player, int count, bool revealCards)
        {
            List<Card> drawn = new List<Card>(count);
            for (int i = 0; i < count; i++)
            {
                Card card = PopCardFromDeck();
                player.AddCardToHand(card);
                drawn.Add(card);
            }
            game.Session.EventManager.OnPlayerDrewFromDeck(player, drawn, revealCards);
            return new ReadOnlyCollection<Card>(drawn);
        }
        /// <summary>
        /// 从牌堆摸牌
        /// </summary>
        /// <param name="player"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public ReadOnlyCollection<Card> PlayerDrawFromDeck(Player player, int count)
        {
            return PlayerDrawFromDeck(player, count, false);
        }

        /// <summary>
        /// 从弃牌堆摸牌
        /// </summary>
        /// <param name="player"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public ReadOnlyCollection<Card> PlayerDrawFromGraveyard(Player player, int count)
        {
            List<Card> drawn = new List<Card>(count);
            for (int i = 0; i < count; i++)
            {
                Card card = PopCardFromGraveyard();
                player.AddCardToHand(card);
                drawn.Add(card);
            }
            game.Session.EventManager.OnPlayerDrewFromGraveyard(player, drawn);
            return new ReadOnlyCollection<Card>(drawn);
        }

        /// <summary>
        /// 把牌移入弃牌堆(弃牌)
        /// </summary>
        /// <param name="card"></param>
        public void PlayerDiscardCard(Card card)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerDiscardedCard(owner, card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 出牌 没有目标
        /// </summary>
        /// <param name="card">要打出的牌</param>
        public void PlayerPlayCard(Card card)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerPlayedCard(owner, card);
            owner.OnPlayedCard(card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 出牌 目标是武将(例如杀.....
        /// </summary>
        /// <param name="card">要打出的牌</param>
        /// <param name="targetPlayer">目标武将</param>
        public void PlayerPlayCard(Card card, Player targetPlayer)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerPlayedCard(owner, card, targetPlayer);
            owner.OnPlayedCard(card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        ///  出牌 目标是牌(例如过河拆桥
        /// </summary>
        /// <param name="card">要打出的牌</param>
        /// <param name="targetCard">目标牌</param>
        public void PlayerPlayCard(Card card, Card targetCard)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerPlayedCard(owner, card, targetCard.Owner, targetCard);
            owner.OnPlayedCard(card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 出牌 当做别的牌打出 
        /// </summary>
        /// <param name="card">要打出的牌</param>
        /// <param name="asCard">要当做的牌</param>
        public void PlayerPlayCard(Card card, CardType asCard)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerPlayedCard(owner, card, asCard);
            owner.OnPlayedCard(card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 出牌 当做别的牌打出 目标是武将
        /// </summary>
        /// <param name="card">要打出的牌</param>
        /// <param name="asCard">要当做的牌</param>
        /// <param name="targetPlayer">目标武将</param>
        public void PlayerPlayCard(Card card, CardType asCard, Player targetPlayer)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerPlayedCard(owner, card, asCard, targetPlayer);
            owner.OnPlayedCard(card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 出牌 当做别的牌打出 目标是牌
        /// </summary>
        /// <param name="card">要打出的牌</param>
        /// <param name="asCard">要当做的牌</param>
        /// <param name="targetPlayer">目标牌</param>
        public void PlayerPlayCard(Card card, CardType asCard, Card targetCard)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerPlayedCard(owner, card, asCard, targetCard.Owner, targetCard);
            owner.OnPlayedCard(card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 装备
        /// </summary>
        /// <param name="card"></param>
        public void PlayerPlayCardOnTable(TableCard card)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();
            if (!owner.RemoveCardFromHand(card))
                throw new InvalidOperationException();

            owner.AddCardToTable(card);
            game.Session.EventManager.OnPlayerPlayedCardOnTable(owner, card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 传递卡片 就是把乐不思蜀闪电等放到别人的判定区
        /// </summary>
        /// <param name="card"></param>
        /// <param name="targetPlayer"></param>
        public void PassTableCard(TableCard card, Player targetPlayer)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();
            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card))
                    throw new InvalidOperationException();

            targetPlayer.AddCardToTable(card);
            game.Session.EventManager.OnPassedTableCard(owner, card, targetPlayer);
        }
        public void PlayerPass(Player player)
        {
            game.Session.EventManager.OnPlayerPassed(player);
        }
        public void PlayerRespondWithCard(Card card)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerRespondedWithCard(owner, card);
            owner.OnRespondedWithCard(card);
            owner.CheckEmptyHand();
        }
        public void PlayerRespondWithCard(Card card, CardType asCard)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();

            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();
            PutCardToGraveyard(card);

            game.Session.EventManager.OnPlayerRespondedWithCard(owner, card, asCard);
            owner.OnRespondedWithCard(card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 从牌堆中弄出n张牌 待选择 (人物技能...五谷丰登...
        /// </summary>
        /// <param name="count"></param>
        /// <param name="owner"></param>
        public void DrawIntoSelection(int count, Player owner)
        {
            List<Card> drawn = new List<Card>(count);
            for (int i = 0; i < count; i++)
            {
                Card card = PopCardFromDeck();
                selection.Add(card);
                drawn.Add(card);
            }
            selectionOwner = owner;
            game.Session.EventManager.OnDrawnIntoSelection(drawn, owner);
        }

        /// <summary>
        ///  从n张里选择一张归入手牌 (人物技能...五谷丰登..
        /// </summary>
        /// <param name="player"></param>
        /// <param name="card"></param>
        public void PlayerPickFromSelection(Player player, Card card)
        {
            if (selectionOwner != null && selectionOwner != player)
                throw new InvalidOperationException();
            if (!selection.Remove(card))
                throw new InvalidOperationException();
            player.AddCardToHand(card);
            game.Session.EventManager.OnPlayerPickedFromSelection(player, card, selectionOwner == null);
        }

        /// <summary>
        /// (n长牌把其中的某张放回牌堆顶
        /// </summary>
        /// <param name="card"></param>
        public void UndrawFromSelection(Card card)
        {
            if (!selection.Remove(card))
                throw new InvalidOperationException();
            PutCardToDeck(card);
            game.Session.EventManager.OnUndrawnFromSelection(card, selectionOwner);
        }
        public void CancelSelection()
        {
            List<Card> selectionCopy = new List<Card>(selection);
            foreach (Card c in selectionCopy)
                CancelCard(c);
        }

        /// <summary>
        /// 偷牌
        /// </summary>
        /// <param name="player"></param>
        /// <param name="card"></param>
        public void PlayerStealCard(Player player, Card card)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();
            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();

            player.AddCardToHand(card);
            game.Session.EventManager.OnPlayerStoleCard(player, owner, card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 玩家把牌移入弃牌堆 (过河拆桥...
        /// </summary>
        /// <param name="player"></param>
        /// <param name="card"></param>
        public void PlayerCancelCard(Player player, Card card)
        {
            Player owner = card.Owner;
            if (owner == null)
                throw new InvalidOperationException();
            if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();

            PutCardToGraveyard(card);
            game.Session.EventManager.OnPlayerCancelledCard(player, owner, card);
            owner.CheckEmptyHand();
        }

        /// <summary>
        /// 从牌堆摸一张牌判定
        /// </summary>
        /// <returns></returns>
        public Card CheckDeck()
        {
            Card checkedCard = PopCardFromDeck();
            PutCardToGraveyard(checkedCard);
            game.Session.EventManager.OnDeckChecked(checkedCard);
            return checkedCard;
        }

        /// <summary>
        /// 判定牌移入弃牌堆 (闪电,乐不思蜀.
        /// </summary>
        /// <param name="card"></param>
        public void CancelCard(Card card)
        {
            Player owner = card.Owner;
            if (owner == null)
            {
                if (!selection.Remove(card))
                    throw new InvalidOperationException();
            }
            else if (!owner.RemoveCardFromHand(card))
                if (!owner.RemoveCardFromTable(card as TableCard))
                    throw new InvalidOperationException();

            PutCardToGraveyard(card);
            game.Session.EventManager.OnCardCancelled(card);
            if (owner != null)
                owner.CheckEmptyHand();
        }

        /// <summary>
        /// 重新生成牌堆
        /// </summary>
        private void RegenerateDeck()
        {
            Card top = graveyard.Pop();
            while (graveyard.Count != 0)
                deck.Push(graveyard.Pop());
            graveyard.Push(top);

            game.Session.EventManager.OnDeckRegenerated();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Card PopCardFromDeck()
        {
            if (deck.Count == 0)
                RegenerateDeck();
            return deck.Pop();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Card PopCardFromGraveyard()
        {
            if (graveyard.Count == 0)
                throw new BadGameStateException();
            return graveyard.Pop();
        }
        private void PutCardToDeck(Card card)
        {
            card.Owner = null;
            deck.Push(card);
        }
        private void PutCardToGraveyard(Card card)
        {
            card.Owner = null;
            graveyard.Push(card);
        }
        private void AddCardToSelection(Card card)
        {
            selection.Add(card);
        }
        private bool RemoveCardFromSelection(Card card)
        {
            return selection.Remove(card);
        }
        

        /// <summary>
        /// 生成牌桌的时候生成牌堆
        /// </summary>
        private void GenerateCards()
        {
            int count = 80;
            if (game.Session.ExEx)
                count += 40;

            cards = new Dictionary<int, Card>(count);
            List<Card> cardList = new List<Card>(count);
            List<int> idList = new List<int>(count);
            for (int id = 1; id <= count; id++)
                idList.Add(id);
            idList.Shuffle();
            Stack<int> idStack = new Stack<int>(idList);

            Card card;
            card = Card.GetCard(game, idStack.Peek(), CardType.闪电, CardSuit.黑桃, CardRank.Ace); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.决斗, CardSuit.黑桃, CardRank.Ace); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.雌雄双股剑, CardSuit.黑桃, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.八卦阵, CardSuit.黑桃, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.顺手牵羊, CardSuit.黑桃, CardRank.Three); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.过河拆桥, CardSuit.黑桃, CardRank.Three); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.顺手牵羊, CardSuit.黑桃, CardRank.Four); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.过河拆桥, CardSuit.黑桃, CardRank.Four); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.青龙偃月刀, CardSuit.黑桃, CardRank.Five); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.绝影, CardSuit.黑桃, CardRank.Five); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.青釭剑, CardSuit.黑桃, CardRank.Six); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.乐不思蜀, CardSuit.黑桃, CardRank.Six); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.黑桃, CardRank.Seven); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.南蛮入侵, CardSuit.黑桃, CardRank.Seven); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.黑桃, CardRank.Eight); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.黑桃, CardRank.Eight); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.黑桃, CardRank.Nine); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.黑桃, CardRank.Nine); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.黑桃, CardRank.Ten); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.黑桃, CardRank.Ten); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.顺手牵羊, CardSuit.黑桃, CardRank.Jack); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.无懈可击, CardSuit.黑桃, CardRank.Jack); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.丈八蛇矛, CardSuit.黑桃, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.过河拆桥, CardSuit.黑桃, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.大宛, CardSuit.黑桃, CardRank.King); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.南蛮入侵, CardSuit.黑桃, CardRank.King); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.万箭齐发, CardSuit.红桃, CardRank.Ace); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃园结义, CardSuit.红桃, CardRank.Ace); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.红桃, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.红桃, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃, CardSuit.红桃, CardRank.Three); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.五谷丰登, CardSuit.红桃, CardRank.Three); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃, CardSuit.红桃, CardRank.Four); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.五谷丰登, CardSuit.红桃, CardRank.Four); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.麒麟弓, CardSuit.红桃, CardRank.Five); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.赤兔, CardSuit.红桃, CardRank.Five); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃, CardSuit.红桃, CardRank.Six); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.乐不思蜀, CardSuit.红桃, CardRank.Six); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃, CardSuit.红桃, CardRank.Seven); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.无中生有, CardSuit.红桃, CardRank.Seven); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃, CardSuit.红桃, CardRank.Eight); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.无中生有, CardSuit.红桃, CardRank.Eight); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃, CardSuit.红桃, CardRank.Nine); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.无中生有, CardSuit.红桃, CardRank.Nine); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.红桃, CardRank.Ten); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.红桃, CardRank.Ten); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.红桃, CardRank.Jack); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.无中生有, CardSuit.红桃, CardRank.Jack); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃, CardSuit.红桃, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.过河拆桥, CardSuit.红桃, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.红桃, CardRank.King); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.爪黄飞电, CardSuit.红桃, CardRank.King); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.诸葛连弩, CardSuit.梅花, CardRank.King); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.决斗, CardSuit.梅花, CardRank.Ace); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.八卦阵, CardSuit.梅花, CardRank.Three); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Four); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.过河拆桥, CardSuit.梅花, CardRank.Five); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Six); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.过河拆桥, CardSuit.梅花, CardRank.Seven); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Eight); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.的卢, CardSuit.梅花, CardRank.Nine); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Ten); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.乐不思蜀, CardSuit.梅花, CardRank.Jack); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.南蛮入侵, CardSuit.梅花, CardRank.King); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Ace); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Three); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Four); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Five); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Six); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Seven); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.梅花, CardRank.Eight); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.借刀杀人, CardSuit.梅花, CardRank.Nine); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.无懈可击, CardSuit.梅花, CardRank.Ten); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.借刀杀人, CardSuit.梅花, CardRank.Jack); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.无懈可击, CardSuit.梅花, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.诸葛连弩, CardSuit.方片, CardRank.Ace); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.决斗, CardSuit.方片, CardRank.Ace); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.顺手牵羊, CardSuit.方片, CardRank.Three); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Three); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.顺手牵羊, CardSuit.方片, CardRank.Four); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Four); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.贯石斧, CardSuit.方片, CardRank.Five); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Five); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.方片, CardRank.Six); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Six); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.方片, CardRank.Seven); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Seven); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.方片, CardRank.Eight); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Eight); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.方片, CardRank.Nine); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Nine); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.方片, CardRank.Ten); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Ten); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Jack); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.闪, CardSuit.方片, CardRank.Jack); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.桃, CardSuit.方片, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.方天画戟, CardSuit.方片, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.杀, CardSuit.方片, CardRank.King); cardList.Add(card); cards.Add(idStack.Pop(), card);
            card = Card.GetCard(game, idStack.Peek(), CardType.紫骍, CardSuit.方片, CardRank.King); cardList.Add(card); cards.Add(idStack.Pop(), card);
      
            if (game.Session.ExEx)
            {
                card = Card.GetCard(game, idStack.Peek(), CardType.寒冰剑, CardSuit.黑桃, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
                card = Card.GetCard(game, idStack.Peek(), CardType.闪电, CardSuit.红桃, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
                card = Card.GetCard(game, idStack.Peek(), CardType.仁王盾 , CardSuit.梅花, CardRank.Two); cardList.Add(card); cards.Add(idStack.Pop(), card);
                card = Card.GetCard(game, idStack.Peek(), CardType.无懈可击, CardSuit.方片, CardRank.Queen); cardList.Add(card); cards.Add(idStack.Pop(), card);
            }
            //card = Card.GetCard(game, idStack.Peek(), CardType., CardSuit., CardRank.); cardList.Add(card); cards.Add(idStack.Pop(), card);

            cardList.Shuffle();
            deck = new Stack<Card>(cardList);
            graveyard = new Stack<Card>(count);
        }
    }
}

